package com.xdog.web3.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xdog.web3.config.Web3ConfigService;
import com.xdog.web3.domain.balance.SpecificTokenBalanceRequest;
import com.xdog.web3.domain.balance.TokenBalanceQuery;
import com.xdog.web3.domain.balance.TotalValueQuery;
import com.xdog.web3.service.IBalanceService;
import com.xdog.web3.utils.Web3ApiUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 余额服务实现类
 */
@Slf4j
@Service
public class BalanceServiceImpl implements IBalanceService {

    @Resource
    private Web3ConfigService web3ConfigService;

    @Override
    public JSONObject getTotalValueByAddress(TotalValueQuery query) {
        String requestPath = "/api/v6/dex/balance/total-value-by-address";

        Map<String, String> params = new HashMap<>();
        params.put("address", query.getAddress());
        params.put("chains", query.getChains());

        if (query.getAssetType() != null) {
            params.put("assetType", query.getAssetType());
        }

        if (query.getExcludeRiskToken() != null) {
            params.put("excludeRiskToken", query.getExcludeRiskToken().toString());
        }

        return Web3ApiUtil.get(requestPath, params);
    }

    @Override
    public JSONObject getAllTokenBalancesByAddress(TokenBalanceQuery query) {
        String requestPath = "/api/v6/dex/balance/all-token-balances-by-address";

        Map<String, String> params = new HashMap<>();
        params.put("address", query.getAddress());
        params.put("chains", query.getChains());

        if (query.getExcludeRiskToken() != null) {
            params.put("excludeRiskToken", query.getExcludeRiskToken());
        }

        return Web3ApiUtil.get(requestPath, params);
    }

    @Override
    public JSONObject getTokenBalancesByAddress(SpecificTokenBalanceRequest request) {
        String requestPath = "/api/v6/dex/balance/token-balances-by-address";
        return Web3ApiUtil.post(requestPath, request);
    }
}
